<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/import.jsp"%>
<%
	if ("login".equals(act)) {
		//用户登录
		String user_name = input.getDesc("user_name", "");
		String password = input.getDesc("password", "");
		password = Tools.getMD5(password);
		List<ManageUser> list = QBuilder.db("basedb").select().from("ManageUser").where(new Where("userName=", user_name).and("password=", password).and("status=1")).query().getResultObj();
		if (list.size() == 0) {
			msg.setErrcodeAndErrmsg(30021, "账号或密码不正确");
			Tools.print(out, msg.toJson());
		} else {
			ManageUser user = list.get(0);
			msg.setErrmsg("登录成功");
			session.setAttribute("roles", getPrivsByUser(user));
			session.setAttribute("menus", getMenusByUser(user));
			session.setAttribute("user", user);
			session.setAttribute("hospital", user.getHospital());
            user.setSessionId(session.getId());
            DBTools.db("basedb").modItem(user,user.getId());
			List<Map> roles = QBuilder.db("basedb").select("new map(r.name as name, r.id as id)").from("UserRole as ur, Role as r").where(new Where("ur.roleId=r.id").and("ur.userId=", user.getId())).query().getResultObj();
			session.setAttribute("user_role", isEmpty(roles)?"未知角色":roles.get(0).get("name"));
			session.setAttribute("user_role_id", isEmpty(roles)?-1:roles.get(0).get("id"));
			Tools.print(out, msg.toJson());
		}
		return;
	}
%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link  href="images/favicon.ico" rel="shortcut icon" type="image/x-icon">
    <link rel="stylesheet" href="css/public.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="css/login.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="css/sweetalert.css">
    <style>
        .ui_input,
        .ui_input_check{
            height: 40px;
            line-height: 30px;
        }
    </style>
</head>
<body>

<!--头部-->
<div style="background:#fff;">
<div class="top_nav_cont">
    <div class="top_nav_logo">
        <img src="images/logo-web.png">
        <span>天下布医 ● 医院管理平台</span>
    </div>
</div>
</div>
<!--中间部分-->
<div class="bg">
    <div class="inner_bg">
        <!--左面的图片-->
        <div class="inner_bg_pic">
            <img src="images/login_picthree.png" alt="">
         </div>
        <!--右面的图片-->
       <div class="inner_bg_login">
               <p class="titl">登录管理平台</p>
               <ul>
                   <li>
                       <input type="text" id="login_name" name='login_name' class="ui_input" value="" placeholder="请输入用户名" title="请输入用户名" required>
                   </li>
                   <li>
                       <input type="password" id="password" name='password' class="ui_input" value="" placeholder="请输入密码" title="请输入密码" required>
                   </li>
                   <li>
                       <input type="text" id="inputCode" class="ui_input_check" placeholder="请输入验证码" title="请输入验证码"  required>
                       <span  class="check_pic" id="code"></span>
                       <div class="clearfix"></div>
                   </li>
                   <li style="margin-bottom: 10px;">
                       <input type="checkbox" class="ui_input_checkbox" id="remember">
                       <span class="rem_pass">记住用户名密码</span>
                       <span class="fresh">看不清?点击验证码可刷新</span>
                       <div class="clearfix"></div>
                   </li>
                   <li>
                       <button class="sure_login"  id="submit">登录</button>
                   </li>
               </ul>
       </div>
    </div>
</div>
<!--结束部分-->
<div class="footer">
    <div class="footer_con1">
        <span>热线电话</span>
        <span class="ui_font">0512-69356650</span>
    </div>
    <div class="footer_con2">
        <span>邮箱</span>
        <span class="ui_font">dev01@tianxiabuyi.com</span>
    </div >
    <div class="footer_con3">
        <span>地址</span>
        <span class="ui_font">苏州高新区竹园路209号财富广场2号楼1908</span>
    </div>
    <div class="clearfix"></div>
</div>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="js/sweetalert-dev.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="js/js.KinerCode.js"></script>
<script src="js/sha1.js" type="text/JavaScript"></script>
<!--图片验证码-->
<script>
    var inp = document.getElementById('inputCode');
    var code = document.getElementById('code');
    var submit = document.getElementById('submit');
    var c = ["+", "-", "*", "/"];
    var arr = [];
    for (var i = 0; i < 1000; i++) {
        var num = parseInt(Math.random() * 100 + 1);
        var num2 = parseInt(Math.random() * 100 + 1);
        var num3 = parseInt(Math.random() * 4);
        if (c[num3] === '/') {
            var x = num % num2;
            if (x != 0) {
                num -= x;
                if(num==0) {
                    var temp = num;
                    num2 = num;
                    num = temp;
                }
            }
        }
        if(num==0&&num==0) {
            continue;
        }
        var str = num + c[num3] + num2;
        arr.push(str);
    }
	function loginVali() {
		var kinerCodeVali = false;
		var c = new KinerCode({
	        len: 4,//需要产生的验证码长度
	        // chars: ["1+2","3+15","6*8","8/4","22-15"],//问题模式:指定产生验证码的词典，若不给或数组长度为0则试用默认字典
	        chars: [
	            1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
	            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
	            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
	        ],//经典模式:指定产生验证码的词典，若不给或数组长度为0则试用默认字典
	        question: false,//若给定词典为算数题，则此项必须选择true,程序将自动计算出结果进行校验【若选择此项，则可不配置len属性】,若选择经典模式，必须选择false
	        copy: false,//是否允许复制产生的验证码
	        bgColor: "",//背景颜色[与背景图任选其一设置]
	        bgImg: "images/bg.jpg",//若选择背景图片，则背景颜色失效
	        randomBg: false,//若选true则采用随机背景颜色，此时设置的bgImg和bgColor将失效
	        inputArea: inp,//输入验证码的input对象绑定【 HTMLInputElement 】
	        codeArea: code,//验证码放置的区域【HTMLDivElement 】
	        click2refresh: true,//是否点击验证码刷新验证码
	        false2refresh: true,//在填错验证码后是否刷新验证码
	        //  validateObj: submit,//触发验证的对象，若不指定则默认为已绑定的输入框inputArea
	        validateEven: "change",//触发验证的方法名，如click，blur等
	        validateFn: function (result, code) {//验证回调函数
				kinerCodeVali = result;
	        }
	    });
		submit.onclick = finalVali;
		code.onclick = function() {
			kinerCodeVali = false;
		}
		document.body.addEventListener("keyup", function() {
			if (window.event.keyCode == 13) {
				finalVali();
			}
		})
		function finalVali() {
			if($('#login_name').val().trim() == "") {
				swal("请输入用户名");
				return;
			} else if($('#password').val() == "") {
                swal("请输入密码");
                return false;
            } else if (kinerCodeVali) {   //在这里加个||true可以在开发环境中不用输验证码
				//存入的时候密码以用户名为salt
				var password = $('#password').val();
				save();
				submit.textContent = "登录中...";
				$.ajax({
					type: 'post',
					url: 'login.jsp',
					data:{
						user_name:$('#login_name').val(),
						password:password,
						act:"login"
					} ,
					dataType: 'json',
					success: function (result) {
                        submit.textContent = "登录";
						if (result.errcode!=0) {
							swal("用户名或者密码错误");
						}
						else {
							location.href = "index.jsp";
						}
					} ,
					error: function (error) {
					  	swal("用户名或者密码错误");
					}
				});
			} else {
			  	swal('验证码不正确');
			}
		}
	}
	loginVali();
    //======================插件引用主体

    //记住用户名密码
  function save() {
    if ($("#remember").prop("checked")) {
      var username = $("#login_name").val();
      var password = $("#password").val();
      $.cookie("rmbUser", "true", { expires: 7 } ); //存储一个带7天期限的cookie
      $.cookie("username", username, { expires: 7 } );
      $.cookie("password", password, { expires: 7 } );
      } else{
      $.cookie("rmbUser", "false", { expire: -1 } );
      $.cookie("username", "", { expires: -1 } );
      $.cookie("password", "", { expires: -1 } );
    }
  } ;
  $(document).ready(function() {
    if ($.cookie("rmbUser") == "true") {
      $("#remember").prop("checked", true);
      $("#login_name").val($.cookie("username"));
      $("#password").val($.cookie("password"));
      }
    } );
</script>
</body>
</html>
